package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.mapper.DictMapper;
import com.crm.context.BaseContext;
import com.crm.dto.DictDto;
import com.crm.entity.Dict;
import com.crm.result.PageResult;
import com.crm.service.DictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper,Dict> implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public PageResult pageDict(DictDto dictDto) {
        Integer pageNum = dictDto.getPageNum() == null ? 1 : dictDto.getPageNum();
        Integer pageSize = dictDto.getPageSize() == null ? 10 : dictDto.getPageSize();

        Page<Dict> page = Page.of(pageNum, pageSize);

        //Page<Dict> page = Page.of(dictDto.getPageNum(), dictDto.getPageSize());

        //分页条件
        LambdaQueryWrapper<Dict> wrapper = new LambdaQueryWrapper<Dict>();
        wrapper.like(dictDto.getDictName()!=null,Dict::getDictName,dictDto.getDictName());
        wrapper.like(dictDto.getDictType()!=null,Dict::getDictType,dictDto.getDictType());
        //
//        if (!CollectionUtils.isEmpty(activityDto.getParams())){
//            wrapper.between(Activity::getCreateTime,activityDto.getParams().get("beginCreateTime"),activityDto.getParams().get("endCreateTime"));
//            wrapper.between(Activity::getActivityTime,activityDto.getParams().get("beginTime"),activityDto.getParams().get("endTime"));
//        }
        if (!CollectionUtils.isEmpty(dictDto.getParams())) {


            // 同样的逻辑处理 ActivityTime
            Object beginTime = dictDto.getParams().get("beginTime");
            Object endTime = dictDto.getParams().get("endTime");

            if (beginTime != null && endTime != null &&
                    !beginTime.toString().trim().isEmpty() &&
                    !endTime.toString().trim().isEmpty()) {
                wrapper.between(Dict::getCreateTime, beginTime, endTime);
            }
        }

        super.page(page,wrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void add(DictDto dictDto) {
        Dict dict = new Dict();
        //设置状态（默认为0）
        dict.setStatus(Dict.DICT_TYPE_STATUS_OPEN);

        BeanUtils.copyProperties(dictDto,dict);
        //设置创建时间
        dict.setCreateTime(LocalDateTime.now().toString());

        //设置创建人
        Long currentId = BaseContext.getCurrentId();
        dict.setCreateBy(currentId != null ? currentId.toString() : null);

        dictMapper.insert(dict);

    }

//

    @Override
    public Dict selectDictById(Long dictId) {
        LambdaQueryWrapper<Dict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dict::getDictId,dictId);
        Dict dict = dictMapper.selectOne(wrapper);
        return dict;
    }

    @Override
    public void updateDict(Dict dict) {
        LambdaQueryWrapper<Dict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dict::getDictId,dict.getDictId());
        dictMapper.update(dict,wrapper);
    }



    @Override
    public void deleteBatchByIds(List<Long> ids) {
        if(!CollectionUtils.isEmpty(ids)){
            for (Long id : ids) {
                dictMapper.delete(Wrappers.<Dict>lambdaQuery().eq(Dict::getDictId, id));
            }
        }

    }
}
